package org.esprit.ocm.client.model.ec2.datasource;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.model.ec2.ImageRecord;
import org.esprit.ocm.dto.ec2.impl.ImageDescriptionDto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ImageEC2LocalDS extends DataSource {

	private static ImageEC2LocalDS	instance	= null;

	public static ImageEC2LocalDS getInstance(List<ImageDescriptionDto> images) {
//		if (instance == null) {
			instance = new ImageEC2LocalDS("imageEC2LocalDS", images);
//		}
		return instance;
	}

	public ImageEC2LocalDS(String id, List<ImageDescriptionDto> images) {

//		setID(id);

		DataSourceTextField idField = new DataSourceTextField("id", "id");
		DataSourceTextField manifestField = new DataSourceTextField("manifest", "manifest");
		DataSourceTextField stateField = new DataSourceTextField("state", "state");
		DataSourceTextField ownerField = new DataSourceTextField("owner", "owner");
		DataSourceTextField visibilityField = new DataSourceTextField("visibility", "visibility");
		DataSourceTextField architectureField = new DataSourceTextField("architecture", "architecture");
		DataSourceTextField kernelId = new DataSourceTextField("kernelId", "kernelId");
		DataSourceTextField ramdiskId = new DataSourceTextField("ramdiskId", "ramdiskId");
		DataSourceTextField imageType = new DataSourceTextField("imageType", "imageType");

		setFields(idField, manifestField, stateField, ownerField, visibilityField, architectureField, kernelId, ramdiskId, imageType);

		List<ListGridRecord> l = new ArrayList<ListGridRecord>();
		for (ImageDescriptionDto from : images) {
			// ImageRecord to = new ImageRecord(i.getImageId(),
			// i.getImageLocation(), i.getImageState(), i.getImageOwnerId(),
			// String.valueOf(i
			// .isPublic()), i.getArchitecture());

			ImageRecord to = new ImageRecord();

			to.setId(from.getImageId());
			to.setManifest(from.getImageLocation());
			to.setState(from.getImageState());
			to.setOwner(from.getImageOwnerId());
			to.setVisibility(String.valueOf(from.isPublic()));
			to.setArchitecture(from.getArchitecture());
			to.setKernelId(from.getKernelId());
			to.setRamdiskId(from.getRamdiskId());
			to.setImageType(from.getImageType());

			l.add(to);
		}

		// setTestData((DataClass[]) imageRecords.toArray());
		setTestData(l.toArray(new ImageRecord[0]));
		setClientOnly(true);
	}

}
